﻿package igz.fleaxy.ui.menu 
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.utils.getQualifiedClassName;
	import flash.utils.Timer;
	import igz.fleaxy.pattern.AbstractFactory;
	import igz.fleaxy.pattern.Dto;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 * 
	 * AbstractMenu es una clase que extiende de Sprite y se utiliza para mostrar un menu con opciones.
	 */
	public class AbstractMenuUi extends Sprite {
		protected var _MenuRelationsColNames:Object = { "ID" : "", "ID_PARENT" : "" };
		protected var _Containers:Object;
		public function get $Containers():Object {
			return _Containers
		};
		
		protected var _ContainerType:Class;
		protected var _ItemType:Class;

		protected var _ContainerActivo:AbstractMenuContainerUi;
		public function get $ContainerActivo():AbstractMenuContainerUi {
			return _ContainerActivo;
		}
		public function set $ContainerActivo( p_containerActivo:AbstractMenuContainerUi ):void {
			_ContainerActivo = p_containerActivo;
		}

		protected var _TimerHide:Timer;
		protected var _RecursiveHide_OnComplete:Function;

		public function AbstractMenuUi(p_containerType:Class, p_itemType:Class ) {
			_ContainerType = p_containerType;
			_ItemType 	   = p_itemType;
			_Containers  = new Object();
			
			_TimerHide = new Timer( 200, 0 );
			_TimerHide.addEventListener( TimerEvent.TIMER, _TimerEvent_RecursiveHide );
		}

		/**
		 * 
		 * @param	p_menu
		 */
		protected function _Load( p_menu:AbstractFactory ) : void {
			if ( _MenuRelationsColNames["ID"] == "" || _MenuRelationsColNames["ID_PARENT"] == "" ) {
				throw new Error("[AbstractMenu][_AddItem] :: Falta el nombre de columna de relación use _MenuRelationsColNames para establecerla");
			}
			else {
				// Create groups and options
				for each ( var optionDto:Dto in p_menu.$Dtos ) {
					_AddItem( optionDto );
				}
				// Create the relationships
				_CreateRelationships();
			}
		}

		/**
		 * 
		 * @param	p_itemDto
		 */
		protected function _AddItem ( p_itemDto:Dto ) : void {
			
			if ( _MenuRelationsColNames["ID"] == "" || _MenuRelationsColNames["ID_PARENT"] == "" ) {
				throw new Error("[AbstractMenu][_AddItem] :: Falta el nombre de columna de relación use _MenuRelationsColNames para establecerla");
			}
			else {
				var idParent : String = p_itemDto[ _MenuRelationsColNames["ID_PARENT"] ];
				if ( _Containers[ idParent ] == null ) {
				   _Containers[ idParent ] = new _ContainerType( this );
				}
				( _Containers[ idParent ] as AbstractMenuContainerUi ).$AddItem( p_itemDto, _ItemType );
			}
		}
				
		protected function _Delete() : void {		
			_Containers = new Object();
			_ContainerActivo = null;
			ObjectUtil.$CleanMemory();
		}

		protected function _CreateRelationships():void {
			var container:AbstractMenuContainerUi;
			var item:AbstractMenuItemUi;
			for each ( container in _Containers ) {
				if ( container is AbstractMenuContainerUi ) {
					for each ( item in container.$Items ) {
						if ( _Containers[item.$ItemDto[_MenuRelationsColNames["ID"]]] != null ) {
							item.$ContainerChild = _Containers[item.$ItemDto[_MenuRelationsColNames["ID"]]];
							(_Containers[item.$ItemDto[_MenuRelationsColNames["ID"]]] as AbstractMenuContainerUi).$ItemParentUi = item;
						}
					}
				}
			}
		}

		protected function _StartRecursiveHide(p_delay:Number = 200, f_onComplete : Function = null) : void {
			_TimerHide.delay = p_delay;
			_RecursiveHide_OnComplete = f_onComplete;
			_TimerHide.start();
		}

		protected function _StopRecursiveHide() : void {
			_TimerHide.stop();
		}

		protected function _TimerEvent_RecursiveHide(p_timerEvent:TimerEvent) : void {
			if ( $ContainerActivo != null ) {
				($ContainerActivo as AbstractMenuContainerUi).$Hide();
				if ( ($ContainerActivo as AbstractMenuContainerUi).$ItemParentUi != null )  {
					$ContainerActivo = ($ContainerActivo as AbstractMenuContainerUi).$ItemParentUi.$MyContainerUi;
				}
				else {
					$ContainerActivo = null;
					if ( _RecursiveHide_OnComplete!=null ) {
						_RecursiveHide_OnComplete();
					}
				}
			}
			else {
				_StopRecursiveHide();
			}
		}

	}
	
}